Imports cmplib01
Imports FMPLib01

'// load and unload rules
'// currently, the system is pretty simple. If a unit is within 40 pixels, it can be transported (if there is enough
'// capacity), regardless of intervening terrain. In the future, it could be changed to pay attention to terrain.
'// loading and then unloading a unit in the same turn will put the unit back into its old location. The actual 
'// position of the transported unit is updated during turn generation.


Public Class frmTransport
    Inherits System.Windows.Forms.Form

    Private c_TRANSPORTDISTANCE As Integer = 30

    Private _transporter As GroundChitType
    Private _flexmap As FlexMapData '// in case I get the crazy idea of drawing shit. Yeah baby!
    Private _unitlist As New GroundChits '// all the units owned by the player. We filter them out. Yeah baby.

    Private selectedChit As GroundChitType
    Friend WithEvents imlTransport As System.Windows.Forms.ImageList
    Private doingUnload As Boolean  '// if we click 'unload' we need to know that

    Public Overloads Sub ShowDialog(ByRef trans As GroundChitType, ByRef flexmap As FlexMapData, ByRef allUnits As GroundChits)
        _transporter = trans

        '// scrub the provided list of units to create our unit list with only the valid units.
        Try
            Dim gc As GroundChitType
            Dim dist As Long

            For Each gc In allUnits
                If gc.CanBeTransported Then
                    Select Case gc.Status
                        Case GroundUnitStatus_cgd.OK, GroundUnitStatus_cgd.Regrouped, GroundUnitStatus_cgd.Moving, GroundUnitStatus_cgd.Assembling, GroundUnitStatus_cgd.InTransport
                            '// these are all ok to transport. Other units won't show.
                            dist = XYDistance(_transporter.Location, gc.Location)
                            '// we can't transport ourself!
                            If gc.ID <> _transporter.ID Then
                                '// we also can't transport a transport that is transporting...
                                If gc.TransportedUnits.Count = 0 Then
                                    If dist <= c_TRANSPORTDISTANCE Then _unitlist.Add(gc)
                                End If
                            End If
                    End Select
                End If
            Next

            '// show the units that are already in transport
            For Each gc In _transporter.TransportedUnits
                lstLoadedUnits.Items.Add(gc)
            Next

        Catch ex As Exception
            HandleException("frmTransport", ex)
        End Try

        MyBase.StartPosition = FormStartPosition.CenterParent

        MyBase.ShowDialog()
    End Sub

    Private Sub ListTransported()
        Try

            If _transporter Is Nothing Then Exit Sub
            Dim gc As GroundChitType

            '// clear the old
            lstLoadedUnits.Items.Clear()

            '// list whats in
            For Each gc In _transporter.TransportedUnits
                lstLoadedUnits.Items.Add(gc)
            Next

        Catch ex As Exception
            HandleException("frmTransport", ex)
        End Try
    End Sub

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlArea As System.Windows.Forms.Panel
    Friend WithEvents lstLoadedUnits As System.Windows.Forms.ListBox
    Friend WithEvents btnLoad As System.Windows.Forms.Button
    Friend WithEvents btnUnload As System.Windows.Forms.Button
    Friend WithEvents btnOk As System.Windows.Forms.Button
    Friend WithEvents lblLocalArea As System.Windows.Forms.Label
    Friend WithEvents pnlTopBorder As System.Windows.Forms.Panel
    Friend WithEvents picCampaignerIcon As System.Windows.Forms.PictureBox
    Friend WithEvents txtTitleBox As System.Windows.Forms.TextBox
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents lblLoadedUnits As System.Windows.Forms.Label
    Friend WithEvents lblSelectedUnit As System.Windows.Forms.Label
    Friend WithEvents lblUnload As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmTransport))
        Me.pnlArea = New System.Windows.Forms.Panel
        Me.lblUnload = New System.Windows.Forms.Label
        Me.lstLoadedUnits = New System.Windows.Forms.ListBox
        Me.btnLoad = New System.Windows.Forms.Button
        Me.btnUnload = New System.Windows.Forms.Button
        Me.btnOk = New System.Windows.Forms.Button
        Me.lblLocalArea = New System.Windows.Forms.Label
        Me.pnlTopBorder = New System.Windows.Forms.Panel
        Me.picCampaignerIcon = New System.Windows.Forms.PictureBox
        Me.txtTitleBox = New System.Windows.Forms.TextBox
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.lblSelectedUnit = New System.Windows.Forms.Label
        Me.lblLoadedUnits = New System.Windows.Forms.Label
        Me.imlTransport = New System.Windows.Forms.ImageList(Me.components)
        Me.pnlArea.SuspendLayout()
        Me.pnlTopBorder.SuspendLayout()
        CType(Me.picCampaignerIcon, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlArea
        '
        Me.pnlArea.BackColor = System.Drawing.Color.Gray
        Me.pnlArea.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlArea.Controls.Add(Me.lblUnload)
        Me.pnlArea.Location = New System.Drawing.Point(16, 24)
        Me.pnlArea.Name = "pnlArea"
        Me.pnlArea.Size = New System.Drawing.Size(160, 158)
        Me.pnlArea.TabIndex = 0
        '
        'lblUnload
        '
        Me.lblUnload.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lblUnload.ForeColor = System.Drawing.Color.Yellow
        Me.lblUnload.Location = New System.Drawing.Point(-1, -1)
        Me.lblUnload.Name = "lblUnload"
        Me.lblUnload.Size = New System.Drawing.Size(161, 19)
        Me.lblUnload.TabIndex = 0
        Me.lblUnload.Text = "Click unload location"
        Me.lblUnload.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.lblUnload.Visible = False
        '
        'lstLoadedUnits
        '
        Me.lstLoadedUnits.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstLoadedUnits.Location = New System.Drawing.Point(184, 24)
        Me.lstLoadedUnits.Name = "lstLoadedUnits"
        Me.lstLoadedUnits.Size = New System.Drawing.Size(155, 158)
        Me.lstLoadedUnits.TabIndex = 0
        '
        'btnLoad
        '
        Me.btnLoad.BackColor = System.Drawing.Color.SteelBlue
        Me.btnLoad.FlatAppearance.BorderColor = System.Drawing.Color.Navy
        Me.btnLoad.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnLoad.Location = New System.Drawing.Point(184, 184)
        Me.btnLoad.Name = "btnLoad"
        Me.btnLoad.Size = New System.Drawing.Size(75, 23)
        Me.btnLoad.TabIndex = 1
        Me.btnLoad.Text = "Load"
        Me.btnLoad.UseVisualStyleBackColor = False
        '
        'btnUnload
        '
        Me.btnUnload.BackColor = System.Drawing.Color.SteelBlue
        Me.btnUnload.FlatAppearance.BorderColor = System.Drawing.Color.Navy
        Me.btnUnload.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnUnload.Location = New System.Drawing.Point(264, 184)
        Me.btnUnload.Name = "btnUnload"
        Me.btnUnload.Size = New System.Drawing.Size(75, 23)
        Me.btnUnload.TabIndex = 2
        Me.btnUnload.Text = "Unload"
        Me.btnUnload.UseVisualStyleBackColor = False
        '
        'btnOk
        '
        Me.btnOk.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnOk.BackColor = System.Drawing.Color.LightSteelBlue
        Me.btnOk.FlatAppearance.BorderColor = System.Drawing.Color.Navy
        Me.btnOk.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOk.Location = New System.Drawing.Point(280, 274)
        Me.btnOk.Name = "btnOk"
        Me.btnOk.Size = New System.Drawing.Size(75, 23)
        Me.btnOk.TabIndex = 3
        Me.btnOk.Text = "OK"
        Me.btnOk.UseVisualStyleBackColor = False
        '
        'lblLocalArea
        '
        Me.lblLocalArea.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblLocalArea.ForeColor = System.Drawing.SystemColors.ControlText
        Me.lblLocalArea.Location = New System.Drawing.Point(16, 0)
        Me.lblLocalArea.Name = "lblLocalArea"
        Me.lblLocalArea.Size = New System.Drawing.Size(160, 23)
        Me.lblLocalArea.TabIndex = 4
        Me.lblLocalArea.Text = "Local Area"
        Me.lblLocalArea.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'pnlTopBorder
        '
        Me.pnlTopBorder.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.pnlTopBorder.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlTopBorder.Controls.Add(Me.picCampaignerIcon)
        Me.pnlTopBorder.Controls.Add(Me.txtTitleBox)
        Me.pnlTopBorder.Location = New System.Drawing.Point(0, 0)
        Me.pnlTopBorder.Name = "pnlTopBorder"
        Me.pnlTopBorder.Size = New System.Drawing.Size(368, 40)
        Me.pnlTopBorder.TabIndex = 17
        '
        'picCampaignerIcon
        '
        Me.picCampaignerIcon.Image = CType(resources.GetObject("picCampaignerIcon.Image"), System.Drawing.Image)
        Me.picCampaignerIcon.Location = New System.Drawing.Point(6, 3)
        Me.picCampaignerIcon.Name = "picCampaignerIcon"
        Me.picCampaignerIcon.Size = New System.Drawing.Size(34, 34)
        Me.picCampaignerIcon.TabIndex = 9
        Me.picCampaignerIcon.TabStop = False
        '
        'txtTitleBox
        '
        Me.txtTitleBox.BackColor = System.Drawing.Color.SteelBlue
        Me.txtTitleBox.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtTitleBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtTitleBox.ForeColor = System.Drawing.Color.WhiteSmoke
        Me.txtTitleBox.Location = New System.Drawing.Point(48, 8)
        Me.txtTitleBox.Multiline = True
        Me.txtTitleBox.Name = "txtTitleBox"
        Me.txtTitleBox.ReadOnly = True
        Me.txtTitleBox.Size = New System.Drawing.Size(312, 26)
        Me.txtTitleBox.TabIndex = 8
        Me.txtTitleBox.TabStop = False
        Me.txtTitleBox.Text = "Transport"
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.Color.LightSteelBlue
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel1.Controls.Add(Me.lblSelectedUnit)
        Me.Panel1.Controls.Add(Me.lblLoadedUnits)
        Me.Panel1.Controls.Add(Me.lblLocalArea)
        Me.Panel1.Controls.Add(Me.pnlArea)
        Me.Panel1.Controls.Add(Me.btnLoad)
        Me.Panel1.Controls.Add(Me.lstLoadedUnits)
        Me.Panel1.Controls.Add(Me.btnUnload)
        Me.Panel1.Location = New System.Drawing.Point(8, 48)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(352, 216)
        Me.Panel1.TabIndex = 18
        '
        'lblSelectedUnit
        '
        Me.lblSelectedUnit.Location = New System.Drawing.Point(16, 184)
        Me.lblSelectedUnit.Name = "lblSelectedUnit"
        Me.lblSelectedUnit.Size = New System.Drawing.Size(160, 23)
        Me.lblSelectedUnit.TabIndex = 6
        Me.lblSelectedUnit.Text = "None selected"
        Me.lblSelectedUnit.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'lblLoadedUnits
        '
        Me.lblLoadedUnits.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblLoadedUnits.ForeColor = System.Drawing.SystemColors.ControlText
        Me.lblLoadedUnits.Location = New System.Drawing.Point(184, 0)
        Me.lblLoadedUnits.Name = "lblLoadedUnits"
        Me.lblLoadedUnits.Size = New System.Drawing.Size(160, 23)
        Me.lblLoadedUnits.TabIndex = 5
        Me.lblLoadedUnits.Text = "Loaded Units"
        Me.lblLoadedUnits.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'imlTransport
        '
        Me.imlTransport.ImageStream = CType(resources.GetObject("imlTransport.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.imlTransport.TransparentColor = System.Drawing.Color.Transparent
        Me.imlTransport.Images.SetKeyName(0, "transport.ico")
        '
        'frmTransport
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(368, 304)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.pnlTopBorder)
        Me.Controls.Add(Me.btnOk)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "frmTransport"
        Me.Text = "Transport"
        Me.pnlArea.ResumeLayout(False)
        Me.pnlTopBorder.ResumeLayout(False)
        Me.pnlTopBorder.PerformLayout()
        CType(Me.picCampaignerIcon, System.ComponentModel.ISupportInitialize).EndInit()
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub pnlArea_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pnlArea.Paint
        DrawLocalArea(e.Graphics)
    End Sub

    Private Sub DrawLocalArea(ByRef g As Graphics)
        If _transporter Is Nothing Then Exit Sub
        If _unitlist Is Nothing Then Exit Sub

        Try
            Dim gc As GroundChitType

            g.Clear(pnlArea.BackColor)

            For Each gc In _unitlist
                '// if we're currently loading, fuggetabouddit.
                Select Case gc.Status
                    Case GroundUnitStatus_cgd.InTransport

                    Case Else
                        DrawChit(g, gc)
                End Select
            Next

            '// the location of the transport unit.
            'g.DrawRectangle(Pens.Green, CInt(pnlArea.Width / 2) - 3, CInt(pnlArea.Height / 2) - 3, 6, 6)
            g.DrawImage(imlTransport.Images(0), CInt(pnlArea.Width / 2) - CInt(imlTransport.Images(0).Width), CInt(pnlArea.Height / 2) - CInt(imlTransport.Images(0).Height))
            g.DrawEllipse(Pens.Green, CInt(pnlArea.Width / 2) - CInt(c_TRANSPORTDISTANCE), CInt(pnlArea.Height / 2) - CInt(c_TRANSPORTDISTANCE), c_TRANSPORTDISTANCE * 2, c_TRANSPORTDISTANCE * 2)

        Catch ex As Exception
            HandleException("frmTransport", ex)
        End Try
    End Sub

    Private Sub pnlArea_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlArea.MouseUp
        If _transporter Is Nothing Then Exit Sub
        If _unitlist Is Nothing Then Exit Sub

        If doingUnload Then
            If Not lstLoadedUnits.SelectedItem Is Nothing Then DoUnload(New Point(e.X, e.Y), lstLoadedUnits.SelectedItem)
            Exit Sub
        End If

        Try
            Dim gc As GroundChitType
            Dim offX As Integer
            Dim offY As Integer
            Dim dist As Long
            Dim lastDist As Long = 5000

            Dim lastselectedchit As GroundChitType = selectedChit

            selectedChit = Nothing

            For Each gc In _unitlist
                offX = gc.Location.X - _transporter.Location.X
                offY = gc.Location.Y - _transporter.Location.Y

                '// then, to match what we draw, bigger them up
                '// okay, for now, we're not "zooming in"
                'offX *= 2
                'offY *= 2

                offX += pnlArea.Width / 2
                offY += pnlArea.Height / 2

                dist = XYDistance(offX, e.X, offY, e.Y)
                If dist < lastDist Then
                    lastDist = dist
                    selectedChit = gc
                End If
            Next

            If Not selectedChit Is Nothing Then
                DrawChit(pnlArea.CreateGraphics, selectedChit, True)
                lblSelectedUnit.Text = selectedChit.Name
            Else
                lblSelectedUnit.Text = "None selected"
            End If

            If Not lastselectedchit Is Nothing Then
                DrawChit(pnlArea.CreateGraphics, lastselectedchit)
            End If

        Catch ex As Exception
            HandleException("frmTransport", ex)
        End Try
    End Sub

    Private Sub DoUnload(ByVal loc As Point, ByRef unit As GroundChitType)
        'Try
        '    Dim center As New Point(pnlArea.Width / 2, pnlArea.Height / 2)

        '    '// if the point is too far away, exit. remember that we are zoomed in by 200% for this. yah.
        '    If XYDistance(loc, center) > (c_TRANSPORTDISTANCE) Then
        '        lblUnload.Text = "Too far away"
        '        Exit Sub
        '    End If

        '    '// determine if the user is trying to load/unload in the same turn
        '    If unit.Status = GroundUnitStatus_cgd.LoadingTransport Then
        '        unit.Status = GroundUnitStatus_cgd.OK
        '        unit.TransportingIn = Nothing
        '        _transporter.TransportedUnits.Remove(unit)
        '    ElseIf unit.Status = GroundUnitStatus_cgd.InTransport Then
        '        unit.Status = GroundUnitStatus_cgd.UnloadingFromTransport
        '        '// modify the location by the offset
        '        Dim offx As Integer = loc.X - (pnlArea.Width / 2)
        '        Dim offy As Integer = loc.Y - (pnlArea.Height / 2)
        '        unit.Location = New Point(_transporter.Location.X + offx, _transporter.Location.Y + offy)
        '        _transporter.TransportedUnits.Remove(unit)
        '    End If


        '    DrawLocalArea(pnlArea.CreateGraphics)
        '    ListTransported()

        '    doingUnload = False
        '    btnUnload.Text = "Unload"

        'Catch ex As Exception
        '    HandleException("frmTransport", ex)
        'End Try
    End Sub

    Private Sub DrawChit(ByRef g As Graphics, ByVal gc As GroundChitType, Optional ByVal selected As Boolean = False)

        'If gc.Status = GroundUnitStatus_cgd.LoadingTransport Then Exit Sub '// gettonouttahere.

        'Dim offX As Integer
        'Dim offY As Integer
        'Dim centerX As Integer = pnlArea.Width / 2
        'Dim centerY As Integer = pnlArea.Width / 2

        'offX = gc.Location.X - _transporter.Location.X
        'offY = gc.Location.Y - _transporter.Location.Y

        ''// we're going to offset the offset so its bigger... huh?
        ''offX *= 2
        ''offY *= 2

        'Dim iconsize As Integer = gc.Icon(cmplib01.IconImageIndex_cgd.Owner).Width

        ''// if we want to halfsize it
        'iconsize /= 2

        'If selected Then
        '    g.FillRectangle(Brushes.Yellow, centerX + offX - CInt(iconsize / 2) - 2, centerY + offY - CInt(iconsize / 2) - 2, iconsize + 4, iconsize + 4)
        'Else
        '    g.FillRectangle(Brushes.Blue, centerX + offX - CInt(iconsize / 2) - 2, centerY + offY - CInt(iconsize / 2) - 2, iconsize + 4, iconsize + 4)
        'End If

        'g.DrawImage(gc.Icon(cmplib01.IconImageIndex_cgd.Owner), centerX + offX - CInt(iconsize / 2), centerY + offY - CInt(iconsize / 2), iconsize, iconsize)

    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Me.Hide()
    End Sub

    Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        'If selectedChit Is Nothing Then Exit Sub

        ''// cost it up.
        'Dim un As UnitType
        'Dim curCost As Integer
        'For Each un In _transporter.TransportedUnits
        '    curCost += un.TransportCost
        'Next

        'If curCost + selectedChit.TransportCost > _transporter.TransportCapacity Then
        '    MessageBox.Show("The transport can't carry this unit, not enough space.", "Not enough transport space", MessageBoxButtons.OK, MessageBoxIcon.Information)
        '    Exit Sub
        'End If

        'selectedChit.Status = GroundUnitStatus_cgd.LoadingTransport
        'selectedChit.TransportingIn = _transporter

        '_transporter.TransportedUnits.Add(selectedChit)
        ''// clear out the selected chit so that we can't add it a second time.
        'selectedChit = Nothing

        'ListTransported()
        'DrawLocalArea(pnlArea.CreateGraphics)

    End Sub

    Private Sub btnUnload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUnload.Click
        If lstLoadedUnits.SelectedItem Is Nothing Then Exit Sub

        Try
            doingUnload = Not doingUnload
            lblUnload.Visible = doingUnload

            If doingUnload Then
                btnUnload.Text = "Cancel"
            Else
                btnUnload.Text = "Unload"
            End If

        Catch ex As Exception
            HandleException("frmTransport", ex)
        End Try

    End Sub

End Class
